#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
#include<execinfo.h>
int      g_sigstop=0;
void signal_stop(int signum)
{

        if(SIGTERM==signum)
	{
		printf("SIGTERM signal detected\n");
	}
	else if(SIGALRM==signum)
	{
		printf("SIGALRM signal detected\n");
		g_sigstop=1;
	}
}
void signal_user(int signum)
{

	if( SIGUSR1==signum)
	{
		printf("SIGUSR1 signal detected\n");
	}
	else if(SIGUSR2==signum)
	{
		printf("SIGUSR2 signal detected\n");
	}
	g_sigstop=1;
}
void signal_code(int signum)
{

	if(SIGBUS==signum)
	{
		printf("SIGBUS signal detected\n");

	}
	else if(SIGILL==signum)
	{
		printf("SIGILL signal detected\n");
	}
	else if(SIGSEGV==signum)
	{
		printf("SIGSEGV signal detected\n");
	}
	exit(-1);
}
int main(int argc,char * argv[])
{
	char                *ptr=NULL;
	struct sigaction    sigact,sigign;
	//方法1
	signal(SIGTERM,signal_stop);
	signal(SIGALRM,signal_stop);
	signal(SIGBUS,signal_code);
	signal(SIGILL,signal_code);
	signal(SIGSEGV,signal_code);
	//方法2
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags=0;
	sigact.sa_handler=signal_user;

	//ignaore signal
	sigemptyset(&sigign.sa_mask);
	sigign.sa_flags=0;
	sigign.sa_handler=SIG_IGN;
	sigaction(SIGINT,&sigign,0);
	sigaction(SIGUSR1,&sigact,0);
	sigaction(SIGUSR2,&sigact,0);
	printf("program start running for 20 seconds...\n");
	alarm(20);
	while(!g_sigstop)
	{
		;
	}
	printf("program start stop running...\n");
	*ptr='h';
	return   0;



}


